package cn.sdormitory.smartdor.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.sdormitory.basedata.vo.BStudentListVo;
import cn.sdormitory.common.utils.StringUtils;
import cn.sdormitory.smartdor.dao.SdAttenceDao;
import cn.sdormitory.smartdor.dao.SdNormalAttenceDao;
import cn.sdormitory.smartdor.entity.SdDeptUnClockCount;
import cn.sdormitory.smartdor.entity.SdNormalAttence;
import cn.sdormitory.smartdor.service.SdNormalAttenceService;
import cn.sdormitory.smartdor.vo.SdNormalAttenceQueryVo;
import cn.sdormitory.sys.dao.SysDictDetailDao;
import cn.sdormitory.sys.entity.SysDictDetail;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @className: SdNormalAttenceServiceImpl
 * @author: Alex
 * @date: 2024/9/11
 **/
@Service
public class SdNormalAttenceServiceImpl implements SdNormalAttenceService {
	
	@Resource
	private SdNormalAttenceDao sdNormalAttenceDao;
	
	@Resource
	private SysDictDetailDao sysDictDetailDao;
	
	@Resource
	private SdAttenceDao sdAttenceDao;
	
	
	@Override
	public IPage<SdNormalAttence> getSdAttenceList(SdNormalAttenceQueryVo sdNormalAttenceQueryVo) {
		// 如果没有传入日期，则查询当天的数据
		String accessDate = sdNormalAttenceQueryVo.getAccessDate();
		if (StringUtils.isBlank(accessDate)) {
			accessDate = DateUtil.today();
		}
		sdNormalAttenceQueryVo.setAccessDate(accessDate);
		
		DateTime startDate = DateUtil.parse(accessDate, "yyyy-MM-dd", Locale.CHINA);
		DateTime endDate = DateUtil.parse(accessDate + " 23:59:59", "yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		
		sdNormalAttenceQueryVo.setStartDate(startDate);
		sdNormalAttenceQueryVo.setEndDate(endDate);

		int startNum = (sdNormalAttenceQueryVo.getPageNum() - 1) * sdNormalAttenceQueryVo.getPageSize();
		sdNormalAttenceQueryVo.setCurPage(startNum);
		Integer count = sdNormalAttenceDao.getNormalAttenceListCount(sdNormalAttenceQueryVo);
		List<SdNormalAttence> normalAttences = sdNormalAttenceDao.getNormalAttenceList(sdNormalAttenceQueryVo);
		if (Objects.isNull(count)) {
			count = 0;
		}
		IPage<SdNormalAttence> page = new Page<>();
		page.setTotal(count);
		page.setRecords(normalAttences);
		return page;
	}
	
	@Override
	public List<SdDeptUnClockCount> getUnClockStuCountByDate(String checkDate) {
		// 先查询出所有部门信息
		QueryWrapper<SysDictDetail> wrapper = new QueryWrapper<>();
		wrapper.eq("dict_type","class_dept").eq("status", "1");
		List<SysDictDetail> sysDictDetail = sysDictDetailDao.selectList(wrapper);
		if (CollectionUtil.isEmpty(sysDictDetail)) {
			return Collections.emptyList();
		}
		
//		List<String> deptIds = sysDictDetail.stream().map(SysDictDetail::getDictValue).collect(Collectors.toList());
		List<SdDeptUnClockCount> unClockCounts = new ArrayList<>();
		for (SysDictDetail dictDetail : sysDictDetail) {
			List<BStudentListVo> students = sdAttenceDao.getAbsenceStudentsByCheckDateAndDeptId(checkDate, dictDetail.getDictValue(), null);
			SdDeptUnClockCount deptUnClockCount = new SdDeptUnClockCount();
			deptUnClockCount.setDeptId(dictDetail.getDictValue());
			deptUnClockCount.setDeptName(dictDetail.getDictLabel());
			deptUnClockCount.setCount(String.valueOf(students.size()));
			unClockCounts.add(deptUnClockCount);
		}
		
		return unClockCounts;
	}
	
	@Override
	public List<BStudentListVo> getUnClockStuListByDeptId(String deptId, String checkDate) {
		List<BStudentListVo> students = sdAttenceDao.getAbsenceStudentsByCheckDateAndDeptId(checkDate, deptId, null);
		return students;
	}
	
	@Override
	public IPage<SdNormalAttence> getWeekendAttenceList(SdNormalAttenceQueryVo sdNormalAttenceQueryVo) {
		// 如果没有传入日期，则查询当天的数据
		String accessDate = sdNormalAttenceQueryVo.getAccessDate();
		if (StringUtils.isBlank(accessDate)) {
			accessDate = DateUtil.today();
		}
		sdNormalAttenceQueryVo.setAccessDate(accessDate);
		
		DateTime startDate = DateUtil.parse(accessDate, "yyyy-MM-dd", Locale.CHINA);
		DateTime endDate = DateUtil.parse(accessDate + " 23:59:59", "yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		
		sdNormalAttenceQueryVo.setStartDate(startDate);
		sdNormalAttenceQueryVo.setEndDate(endDate);
		
		int startNum = (sdNormalAttenceQueryVo.getPageNum() - 1) * sdNormalAttenceQueryVo.getPageSize();
		sdNormalAttenceQueryVo.setCurPage(startNum);
		Integer count = sdNormalAttenceDao.getWeekendAttenceListCount(sdNormalAttenceQueryVo);
		List<SdNormalAttence> normalAttences = sdNormalAttenceDao.getWeekendAttenceList(sdNormalAttenceQueryVo);
		if (Objects.isNull(count)) {
			count = 0;
		}
		IPage<SdNormalAttence> page = new Page<>();
		page.setTotal(count);
		page.setRecords(normalAttences);
		return page;
	}
}
